import { IFormState, ITableData } from "./types.ts";
import { ITableCHandle, ITableColumn } from "@/utils/types/table.ts";
import { computed, reactive } from "vue";
import { FormOption } from "@/components/autoForm/types/componentTypes.ts";
import { IDictRes } from "@/utils/types/TRequest.ts";
import { validatePhone } from "@/utils/validate.ts";

export const serchOptions = [
  { label: "起止时间", value: "create", type: "timeRange" },
];

export const columns: ITableColumn[] = [
  {
    title: "部门编号",
    dataIndex: "deptId",
    key: "deptId",
  },
  {
    title: "部门名称",
    dataIndex: "deptName",
    key: "deptName",
  },
  {
    title: "部门类型",
    dataIndex: "deptType",
    key: "deptType",
  },
  {
    title: "联系人",
    dataIndex: "leader",
    key: "leader",
  },
  {
    title: "联系电话",
    dataIndex: "phone",
    key: "phone",
  },
  {
    title: "最后修改时间",
    dataIndex: "updateTime",
    key: "updateTime",
  },
  {
    title: "操作",
    dataIndex: "operation",
    key: "operation",
    fixed: "right",
    handle: [
      {
        title: "增加",
        hasPermi: ["system:dept:edit"],
        normal: false, // 正常显示
        showkey: [["type"], ["COMPANY"]],
        funp: ["handleAdd", "add"], // [操作方法，新增或编辑]
      },
      {
        title: "修改",
        hasPermi: ["system:dept:edit"],
        normal: true, // 正常显示
        funp: ["handleAdd", "update"], // [操作方法，新增或编辑]
      },
      {
        title: "删除",
        hasPermi: ["system:dept:remove"],
        normal: true, // 正常显示
        funp: ["handleDelete", "/system/dept"], // [操作方法，新增或编辑]
      },
    ] as ITableCHandle[],
  },
];

export const addOptions = computed<FormOption<IFormState>[]>(() => {
  const o: FormOption<IFormState>[] = [
    {
      label: "上级部门",
      field: "parentId",
      type: "custom",
      rules: [{ required: true, message: "请选择上级" }],
    },
    {
      label: "部门类型",
      field: "type",
      type: "loadSelect",
      props: {
        url: "/common/getEnumList/DeptType",
        handle: (data: IDictRes) => {
          return {
            value: data.name,
            label: data.message,
          };
        },
      },
      rules: [{ required: true, message: "请选择部门类型" }],
    },
    {
      label: "部门名称",
      field: "deptName",
      type: "input",
      rules: [{ required: true, message: "请输入部门名称" }],
    },
    {
      label: "联系人",
      field: "leader",
      type: "input",
      rules: [{ required: true, message: "请输入联系人" }],
    },
    {
      label: "联系电话",
      field: "phone",
      type: "input",
      rules: [
        { required: true, message: "请输入联系人电话" },
        { validator: validatePhone },
      ],
    },
  ];
  return o;
});

export const initFormData: Partial<IFormState> = {
  parentId: null,
  type: "",
  deptName: "",
  deptId: null,
  leader: "",
  phone: "",
};

export const formState = reactive<IFormState>(
  JSON.parse(JSON.stringify(initFormData)),
);

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 */
export function handleTree(data: ITableData[]): ITableData[] {
  //对源数据深度克隆
  const cloneData = JSON.parse(JSON.stringify(data));
  //循环所有项
  const treeData = cloneData.filter((father: ITableData) => {
    const branchArr = cloneData.filter((child: ITableData) => {
      //返回每一项的子级数组
      return father.deptId === child.parentId;
    });
    branchArr.length > 0 ? (father.children = branchArr) : "";
    //返回第一层
    return father.parentId === 0;
  });
  return treeData !== "" ? treeData : data;
}

/**
 * children[]时不显示children
 * @param {Array} data tree结构数据
 */
export function handleTreeFormat(data: ITableData[]): ITableData[] {
  data.forEach((item: ITableData) => {
    if (item.children && item.children.length) {
      handleTreeFormat(item.children);
    } else {
      delete item.children;
    }
  });

  return data;
}
